--- title: Testing Diffusion Flow Embedder on Circle and Swiss Roll keywords: fastai sidebar: home_sidebar nb_path: "02d02a Testing Diffusion Flow Embedder on Circle and Swiss Roll.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
from directed_graphs.datasets import directed_circle, directed_cylinder, directed_spiral, directed_swiss_roll, directed_spiral_uniform, directed_swiss_roll_uniform
from directed_graphs.datasets import plot_directed_2d, plot_directed_3d
from directed_graphs.diffusion_flow_embedding import DiffusionFlowEmbedder
import torch
import numpy as np
{% endraw %} {% raw %}
if torch.__version__[:4] == "1.13":
	# device = torch.device('cuda' if torch.cuda.is_available() else 'mps' if torch.has_mps else 'cpu')
	device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

else:
	device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device
device(type='cpu')
{% endraw %}

Flat Circle

{% raw %}
X, flow, labels = directed_circle(num_nodes=300, radius=1)
plot_directed_2d(X, flow, labels)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,
flow,
t=1,
sigma_graph=15,
sigma_embedding=15,
device=device, 
autoencoder_shape = [50,10],
flow_artist_shape=[30,20,10],
flow_strength_graph=2, 
flow_strength_embedding=5, 
learnable_flow_strength=True,
weight_of_flow = 0.5,
learning_rate=0.001
)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
  0%|          | 0/2000 [00:00<?, ?it/s]
EPOCH 0. Loss 667.5196533203125. Flow strength 4.999000072479248. Weight of flow 0.5 Heatmap of P embedding is 
  5%|▌         | 100/2000 [00:03<01:08, 27.90it/s]
EPOCH 100. Loss 667.2808227539062. Flow strength 4.967296123504639. Weight of flow 0.5 Heatmap of P embedding is 
 10%|▉         | 199/2000 [00:07<01:05, 27.63it/s]
EPOCH 200. Loss 667.1341552734375. Flow strength 4.93806791305542. Weight of flow 0.5 Heatmap of P embedding is 
 15%|█▍        | 298/2000 [00:11<01:01, 27.88it/s]
EPOCH 300. Loss 667.22314453125. Flow strength 4.954817295074463. Weight of flow 0.5 Heatmap of P embedding is 
 20%|██        | 400/2000 [00:15<00:58, 27.57it/s]
EPOCH 400. Loss 667.0772094726562. Flow strength 4.9956231117248535. Weight of flow 0.5 Heatmap of P embedding is 
 25%|██▍       | 499/2000 [00:18<00:55, 27.13it/s]
EPOCH 500. Loss 667.2047119140625. Flow strength 4.947875022888184. Weight of flow 0.5 Heatmap of P embedding is 
 30%|██▉       | 599/2000 [00:22<00:49, 28.33it/s]
EPOCH 600. Loss 667.1126098632812. Flow strength 4.940506458282471. Weight of flow 0.5 Heatmap of P embedding is 
 35%|███▍      | 698/2000 [00:26<00:46, 28.17it/s]
EPOCH 700. Loss 667.0491943359375. Flow strength 4.942741394042969. Weight of flow 0.5 Heatmap of P embedding is 
 40%|███▉      | 798/2000 [00:29<00:42, 28.54it/s]
EPOCH 800. Loss 666.89501953125. Flow strength 4.8559722900390625. Weight of flow 0.5 Heatmap of P embedding is 
 45%|████▌     | 900/2000 [00:33<00:38, 28.49it/s]
EPOCH 900. Loss 666.8792724609375. Flow strength 4.737636566162109. Weight of flow 0.5 Heatmap of P embedding is 
 50%|█████     | 1000/2000 [00:37<00:35, 28.23it/s]
EPOCH 1000. Loss 666.8531494140625. Flow strength 4.592820644378662. Weight of flow 0.5 Heatmap of P embedding is 
 55%|█████▍    | 1099/2000 [00:41<00:32, 28.08it/s]
EPOCH 1100. Loss 666.8324584960938. Flow strength 4.535269260406494. Weight of flow 0.5 Heatmap of P embedding is 
 60%|█████▉    | 1198/2000 [00:44<00:28, 27.98it/s]
EPOCH 1200. Loss 666.767333984375. Flow strength 4.416861057281494. Weight of flow 0.5 Heatmap of P embedding is 
 65%|██████▌   | 1300/2000 [00:48<00:24, 28.06it/s]
EPOCH 1300. Loss 666.7271728515625. Flow strength 4.333467483520508. Weight of flow 0.5 Heatmap of P embedding is 
 70%|██████▉   | 1399/2000 [00:52<00:22, 26.93it/s]
EPOCH 1400. Loss 666.6425170898438. Flow strength 4.193497180938721. Weight of flow 0.5 Heatmap of P embedding is 
 75%|███████▍  | 1498/2000 [00:55<00:18, 27.51it/s]
EPOCH 1500. Loss 666.6141967773438. Flow strength 4.126711368560791. Weight of flow 0.5 Heatmap of P embedding is 
 80%|████████  | 1600/2000 [00:59<00:14, 27.16it/s]
EPOCH 1600. Loss 666.6101684570312. Flow strength 4.069742202758789. Weight of flow 0.5 Heatmap of P embedding is 
 85%|████████▍ | 1699/2000 [01:03<00:10, 28.38it/s]
EPOCH 1700. Loss 666.6130981445312. Flow strength 3.9976584911346436. Weight of flow 0.5 Heatmap of P embedding is 
 90%|████████▉ | 1798/2000 [01:07<00:07, 27.36it/s]
EPOCH 1800. Loss 666.6144409179688. Flow strength 3.9117178916931152. Weight of flow 0.5 Heatmap of P embedding is 
 95%|█████████▌| 1900/2000 [01:11<00:03, 27.13it/s]
EPOCH 1900. Loss 666.615234375. Flow strength 3.829695463180542. Weight of flow 0.5 Heatmap of P embedding is 
100%|██████████| 2000/2000 [01:14<00:00, 26.69it/s]
Exiting training with loss  tensor(666.6058, grad_fn=<AddBackward0>)

{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %}

Tilted Circle

{% raw %}
X, flow, labels = directed_circle(num_nodes=1000, radius=1, ytilt=np.pi/4)
plot_directed_3d(X, flow, labels, mask_prob=0.5)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,
flow,
t=1,
sigma_graph=15,
sigma_embedding=15,
device=device, 
autoencoder_shape = [20,10],
flow_artist_shape=[30,20,10],
flow_strength_graph=2, 
flow_strength_embedding=5, 
learnable_flow_strength=True,
weight_of_flow = 0.5,
learning_rate=0.01
)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
  0%|          | 0/2000 [00:00<?, ?it/s]
EPOCH 0. Loss 667.5437622070312. Flow strength 4.989999771118164. Weight of flow 0.5 Heatmap of P embedding is 
  5%|▌         | 100/2000 [00:03<01:05, 28.83it/s]
EPOCH 100. Loss 667.2559204101562. Flow strength 4.8423686027526855. Weight of flow 0.5 Heatmap of P embedding is 
 10%|█         | 200/2000 [00:07<01:02, 28.72it/s]
EPOCH 200. Loss 666.7958374023438. Flow strength 4.44754695892334. Weight of flow 0.5 Heatmap of P embedding is 
 15%|█▍        | 299/2000 [00:10<00:58, 28.86it/s]
EPOCH 300. Loss 666.6117553710938. Flow strength 3.6958649158477783. Weight of flow 0.5 Heatmap of P embedding is 
 20%|█▉        | 399/2000 [00:14<00:55, 29.10it/s]
EPOCH 400. Loss 666.5806274414062. Flow strength 3.005265712738037. Weight of flow 0.5 Heatmap of P embedding is 
 25%|██▌       | 500/2000 [00:18<00:53, 28.17it/s]
EPOCH 500. Loss 666.5630493164062. Flow strength 2.4203834533691406. Weight of flow 0.5 Heatmap of P embedding is 
 30%|██▉       | 599/2000 [00:21<00:49, 28.45it/s]
EPOCH 600. Loss 666.5516967773438. Flow strength 1.9887703657150269. Weight of flow 0.5 Heatmap of P embedding is 
 35%|███▍      | 698/2000 [00:25<00:46, 28.12it/s]
EPOCH 700. Loss 666.5470581054688. Flow strength 1.7977397441864014. Weight of flow 0.5 Heatmap of P embedding is 
 40%|████      | 800/2000 [00:29<00:42, 28.34it/s]
EPOCH 800. Loss 666.5475463867188. Flow strength 1.6819127798080444. Weight of flow 0.5 Heatmap of P embedding is 
 45%|████▍     | 899/2000 [00:33<00:39, 27.95it/s]
EPOCH 900. Loss 666.5535888671875. Flow strength 1.6262315511703491. Weight of flow 0.5 Heatmap of P embedding is 
 50%|████▉     | 998/2000 [00:36<00:34, 28.69it/s]
EPOCH 1000. Loss 666.55712890625. Flow strength 1.5920469760894775. Weight of flow 0.5 Heatmap of P embedding is 
 55%|█████▌    | 1100/2000 [00:40<00:33, 26.82it/s]
EPOCH 1100. Loss 666.551025390625. Flow strength 1.588066816329956. Weight of flow 0.5 Heatmap of P embedding is 
 60%|█████▉    | 1199/2000 [00:44<00:27, 28.66it/s]
EPOCH 1200. Loss 666.5385131835938. Flow strength 1.5636647939682007. Weight of flow 0.5 Heatmap of P embedding is 
 65%|██████▍   | 1298/2000 [00:47<00:25, 28.07it/s]
EPOCH 1300. Loss 666.5369262695312. Flow strength 1.5073577165603638. Weight of flow 0.5 Heatmap of P embedding is 
 70%|███████   | 1400/2000 [00:51<00:20, 28.96it/s]
EPOCH 1400. Loss 666.5347900390625. Flow strength 1.4788708686828613. Weight of flow 0.5 Heatmap of P embedding is 
 75%|███████▌  | 1500/2000 [00:55<00:17, 28.97it/s]
EPOCH 1500. Loss 666.5307006835938. Flow strength 1.4599934816360474. Weight of flow 0.5 Heatmap of P embedding is 
 80%|████████  | 1600/2000 [00:58<00:13, 28.75it/s]
EPOCH 1600. Loss 666.5322265625. Flow strength 1.4712326526641846. Weight of flow 0.5 Heatmap of P embedding is 
 85%|████████▌ | 1700/2000 [01:02<00:10, 29.02it/s]
EPOCH 1700. Loss 666.5331420898438. Flow strength 1.4765619039535522. Weight of flow 0.5 Heatmap of P embedding is 
 90%|████████▉ | 1799/2000 [01:06<00:07, 28.32it/s]
EPOCH 1800. Loss 666.5335693359375. Flow strength 1.4726852178573608. Weight of flow 0.5 Heatmap of P embedding is 
 95%|█████████▍| 1898/2000 [01:09<00:03, 28.19it/s]
EPOCH 1900. Loss 666.533203125. Flow strength 1.47734534740448. Weight of flow 0.5 Heatmap of P embedding is 
100%|██████████| 2000/2000 [01:13<00:00, 27.16it/s]
Exiting training with loss  tensor(666.5333, grad_fn=<AddBackward0>)

{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %}

Cylinder

{% raw %}
X, flow, labels = directed_cylinder(num_nodes=1000, radius=1, height=20, xtilt=np.pi/4, ytilt=0)
plot_directed_3d(X, flow, labels, mask_prob=0.5)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,flow,t=1,sigma_graph=1,sigma_embedding=1,flow_strength_graph=2

device=device)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
100%|██████████| 2000/2000 [00:29<00:00, 67.56it/s]
Exiting training with loss  tensor(4005.0149, device='cuda:0', grad_fn=<AddBackward0>)

{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %}

Flat Spiral

{% raw %}
X, flow, labels = directed_spiral(num_nodes=1000, num_spirals=2.5, radius=1)
plot_directed_2d(X, flow, labels)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,flow,t=1,sigma_graph=10,sigma_embedding=15,
device=device,
learning_rate=0.001,
weight_of_flow=1)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=10000)
  0%|          | 0/10000 [00:00<?, ?it/s]
EPOCH 0. Loss 255.82760620117188. Flow strength 4.999000072479248. Weight of flow 1 Heatmap of P embedding is 
  1%|          | 100/10000 [00:33<54:47,  3.01it/s]
EPOCH 100. Loss 254.57586669921875. Flow strength 5.101325988769531. Weight of flow 1 Heatmap of P embedding is 
  2%|▏         | 161/10000 [00:55<55:48,  2.94it/s]  
The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.
Canceled future for execute_request message before replies were done
{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %}

Tilted Spiral

{% raw %}
X, flow, labels = directed_spiral(num_nodes=100, num_spirals=2.5, radius=1, xtilt=np.pi/4)
plot_directed_3d(X, flow, labels, mask_prob=0.5)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,flow,t=1,sigma_graph=15,sigma_embedding=15,device=device)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
100%|██████████| 2000/2000 [00:28<00:00, 69.69it/s]
Exiting training with loss  tensor(191.4982, device='cuda:0', grad_fn=<AddBackward0>)

{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %}

Swiss Roll

{% raw %}
X, flow, labels = directed_swiss_roll(num_nodes=1000, num_spirals=2.5, radius=1, height=20, xtilt=np.pi/4, ytilt=0)
plot_directed_3d(X, flow, labels, mask_prob=0.5)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,flow,t=1,sigma_graph=15,sigma_embedding=15,device=device)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
100%|██████████| 2000/2000 [00:29<00:00, 68.07it/s]
Exiting training with loss  tensor(2477.8306, device='cuda:0', grad_fn=<AddBackward0>)

{% endraw %} {% raw %}
dfe.visualize_points(labels)
No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
{% endraw %} {% raw %}
import phate
phate_operator = phate.PHATE(n_components = 2, knn=5, decay=20, t=150)
phate_X = phate_operator.fit_transform(X.cpu())
phate.plot.scatter2d(phate_X, c=labels)
Calculating PHATE...
  Running PHATE on 1000 observations and 3 variables.
  Calculating graph and diffusion operator...
    Calculating KNN search...
    Calculated KNN search in 0.04 seconds.
    Calculating affinities...
    Calculated affinities in 0.02 seconds.
  Calculated graph and diffusion operator in 0.07 seconds.
  Calculating diffusion potential...
  Calculated diffusion potential in 0.17 seconds.
  Calculating metric MDS...
  Calculated metric MDS in 2.06 seconds.
Calculated PHATE in 2.32 seconds.
/tmp/ipykernel_223466/2472049010.py:4: DeprecationWarning: Call to deprecated function (or staticmethod) scatter2d. (Use scprep.plot.scatter2d instead) -- Deprecated since version 1.0.0.
  phate.plot.scatter2d(phate_X, c=labels)
<AxesSubplot:>
{% endraw %}

Uniform Swiss Roll

{% raw %}
X, flow, labels = directed_swiss_roll_uniform(num_nodes=1000, num_spirals=2.5, radius=1, height=20, xtilt=0, ytilt=0)
plot_directed_3d(X, flow, labels, mask_prob=0.5)
{% endraw %} {% raw %}
X = torch.tensor(X)
flow = torch.tensor(flow)
X = X.float().to(device)
flow = flow.float().to(device)
C:\Users\nhiin\AppData\Local\Temp\ipykernel_6980\3651268102.py:1: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  X = torch.tensor(X)
C:\Users\nhiin\AppData\Local\Temp\ipykernel_6980\3651268102.py:2: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
  flow = torch.tensor(flow)
{% endraw %} {% raw %}
dfe = DiffusionFlowEmbedder(X,flow,t=1,sigma_graph=15,sigma_embedding=15,device=device)
dfe = dfe.to(device)
embeddings = dfe.fit(n_steps=2000)
{% endraw %} {% raw %}
dfe.visualize_points(labels)
{% endraw %}